package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-02-19
 */
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private IProductUnitService productUnitService;


    /**
     * 分页查询商品单位
     *
     * @param name
     * @return
     */
    @Operation(summary = "分页查询商品单位")
    @GetMapping("/list")
    //TODO 省略权限，日志
    public TableDataInfo list(String name) {
        startPage();
        List<ProductUnit> list = productUnitService.selectList(name);
        return getDataTable(list);
    }

    /**
     * 保存商品单位
     *
     * @param productUnit
     * @return
     */
    @PostMapping
    @Operation(summary = "保存商品单位")
    public AjaxResult add(@RequestBody ProductUnit productUnit) {
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.save(productUnit));
    }


    @Operation(summary = "根据ID查询商品单位")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productUnitService.getById(id));
    }


    /**
     * 保存商品单位
     *
     * @param productUnit
     * @return
     */
    @PutMapping
    @Operation(summary = "更新商品单位")
    public AjaxResult update(@RequestBody ProductUnit productUnit) {
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateById(productUnit));
    }


    @Operation(summary = "根据ID删除商品单位")
    @DeleteMapping("/{idList}")
    public AjaxResult delete(@PathVariable List<Long> idList) {
        return toAjax(productUnitService.removeBatchByIds(idList));
    }


    @Operation(summary = "查询所有商品单元（计量单位）")
    @GetMapping("/getUnitAll")
    public AjaxResult getUnitAll() {
        List<ProductUnit> list = productUnitService.selectList(null);
        return AjaxResult.success(list);
    }
}
